import Dialog from '@vant/weapp/dialog/dialog';
import { getLocalStorage, validateTel } from '../../utils.js'
Component({
  properties: {
    imageSrc: {
      type: String,
      value: ""
    },
    scenicName: {
      type: String,
      value: ""
    },
    totalNumber: {
      type: Number,
      value: 0
    },
    price: {
      type: Number,
      value: 0
    },
    totalPrice: {
      type: Number,
      value: 0
    },
    scenicPrice: {
      type: Number,
      value: 0
    },
    goodsNumber: {
      type: Number,
      value: 0
    }
  },
  lifetimes: {
    ready() {
      this.setData({ date: this.formatDate(new Date()) })
    }
  },
  data: {
    columns: ['余额支付', '微信支付'],
    phoneValue: '',
    date: '',
    show: false,
    payShow: false,
    dialogShow: false,
    payValue: "余额支付"
  },
  methods: {
    confirmHandle(e){
      console.log(e);
      this.setData({
        payShow: false,
        payValue: e.detail.value
      })
    },
    onPhoneChange(e) {
      this.setData({ phoneValue: e.detail })
    },
    onDisplay() {
      console.log(this.data.date);
      this.setData({ show: true });
    },
    onClose() {
      this.setData({ show: false });
    },
    formatDate(date) {
      const d = new Date(date)
      return `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`
    },
    onConfirm(event) {
      this.setData({ show: false, date: this.formatDate(event.detail) })
    },
    showPopup() {
      this.setData({ payShow: true });
    },
    onPayClose() {
      this.setData({ payShow: false });
    },
    jump_Pay() {
      this.setData({ dialogShow: true });
    },
    onDialogClose() {
      this.setData({ dialogShow: false });
    },
    async onDialogConfirm() {
      // 检验手机号码（检验表单输入的有效性）
      const pv = this.data.phoneValue
      if (!validateTel(pv)) {
        return wx.showToast({ title: pv ? '手机号格式错误' : '手机号必填', icon: 'error' })
      }
      wx.showLoading({ title: '加载中', mask: true })
      // 获取剩余金额 将剩余金额减去支出金额等于剩余金额 更新剩余金额
      const openid = await getLocalStorage('openid')
      let money = null
      if (!!openid) {
        const { result } = await wx.cloud.callFunction({ name: 'rh', data: { type: 'getTotalMoney', openid } })
        if (!!result) {
          money = result.money
        }
      }
      const price = this.data.totalPrice // 未乘以数量
      const fundBalance = money - price
      if (money === null || fundBalance < 0) {
        wx.hideLoading()
        wx.showToast({ title: '余额不足', icon: 'error' })
        return
      }
      await wx.cloud.callFunction({ name: 'rh', data: { type: 'updateTotalMoney', openid, money: fundBalance }})
      // 更新订单列表
      await wx.cloud.callFunction({ name: 'quickstartFunctions', data: { type: 'addOrder', 
        openid,
        money: price,
        phone: this.data.phoneValue,
        date: this.data.date,
        project: this.properties.scenicName,
        status: 1
      }})
      wx.hideLoading()
      wx.showToast({ title: '支付成功', icon: 'success' })
      setTimeout(() => {
        wx.navigateBack({ delta: 2 })
      }, 1000)
    }
  },
})
